/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.transport;

import org.jetbrains.annotations.NotNull;

/**
 * A result of {@link ITransport#send(io.sentry.SentryEnvelope)}. Note that this class is
 * intentionally not subclassable and has only two factory methods to capture the 2 possible states
 * - success or error.
 *
 * @since 2021-06-02
 */
public abstract class TransportResult {
    /**
     * Use this method to announce success of sending the event.
     *
     * @return a successful transport result
     */
    public static @NotNull TransportResult success() {
        return SuccessTransportResult.INSTANCE;
    }

    /**
     * Use this method to announce failure of sending the event.
     *
     * @param responseCode the HTTP status code if known, -1 otherwise
     * @return an erroneous transport result
     */
    public static @NotNull TransportResult error(final int responseCode) {
        return new ErrorTransportResult(responseCode);
    }

    /**
     * Use this method to announce failure of sending the event. Defaults responseCode to -1 (unknown
     * responseCode)
     *
     * @return an erroneous transport result
     */
    public static @NotNull TransportResult error() {
        return error(-1);
    }

    private TransportResult() {
    }

    /**
     * isSuccess
     *
     * @return isSuccess
     */
    public abstract boolean isSuccess();

    /**
     * getResponseCode
     *
     * @return ResponseCode
     */
    public abstract int getResponseCode();

    /**
     * SuccessTransportResult
     *
     * @since 2021-06-02
     **/
    private static final class SuccessTransportResult extends TransportResult {
        static final SuccessTransportResult INSTANCE = new SuccessTransportResult();

        @Override
        public boolean isSuccess() {
            return true;
        }

        @Override
        public int getResponseCode() {
            return -1;
        }
    }

    /**
     * ErrorTransportResult
     *
     * @since 2021-06-02
     **/
    private static final class ErrorTransportResult extends TransportResult {
        private final int responseCode;

        ErrorTransportResult(final int responseCode) {
            this.responseCode = responseCode;
        }

        @Override
        public boolean isSuccess() {
            return false;
        }

        @Override
        public int getResponseCode() {
            return responseCode;
        }
    }
}
